from typing import Dict

from ..dao import list_runs, get_run, update_run
from ..managers.engine_manager import stop_run


def list_runs_service() -> Dict[str, list]:
    return {"items": list_runs()}


def get_run_service(run_id: int) -> Dict | None:
    return get_run(run_id)


def stop_run_service(run_id: int) -> Dict[str, object]:
    from datetime import datetime, timezone
    active = stop_run(run_id)
    if not active:
        update_run(run_id, status='stopped', finished_at=datetime.now(timezone.utc).replace(tzinfo=None).isoformat())
        return {"stopped": True, "note": "runner not active"}
    else:
        update_run(run_id, status='stopped')
        return {"stopped": True}